<template :key="refreshKey">
  <el-form ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px" style="margin-left: 90px;">
    <el-form-item v-if="!userInfo.orgId" label="机构">
      <select-org-tree :value="queryParams.orgId" class="width-120" @orgNodeClick="orgNodeClick"/>
    </el-form-item>
    <el-form-item label="权限" prop="authorityType">
      <el-select v-model="queryParams.authorityType" class="width-120" placeholder="请选择" @change="handleQuery">
        <el-option v-if="userInfo.customerType != '3'" label="所有" value="">所有</el-option>
        <el-option v-for="dict in dictDataMap[
                    'template_authority_type'
                ]" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"/>
      </el-select>
    </el-form-item>
    <el-form-item label="模板名称" prop="title">
      <el-input v-model="queryParams.title" clearable placeholder="请输入模板主题名称" @keyup.enter="handleQuery"/>
    </el-form-item>
    <el-form-item>
      <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
    </el-form-item>
  </el-form>
  <el-table v-adaptive="adaptiveHeight" :border="true" :data="templateMasterList" :header-cell-style="$headerCellStyle" :stripe="true"
            highlight-current-row @row-click="rowClick" @selection-change="handleSelectionChange">
    <el-table-column :align="'center'" type="selection" width="55"/>
    <el-table-column :align="'center'" label="权限" prop="authorityType">
      <template #default="scope">
        <dict-data-tag :options="dictDataMap['template_authority_type']" :value="scope.row.authorityType"/>
      </template>
    </el-table-column>
    <el-table-column :align="'center'" label="模板归属人" prop="userName"/>
    <el-table-column :align="'center'" label="模板类型" prop="type">
      <template #default="scope">
        <dict-data-tag :options="dictDataMap['template_type']" :value="scope.row.type"/>
      </template>
    </el-table-column>
    <el-table-column :align="'center'" label="处方类型" prop="itemClass">
      <template #default="scope">
        <dict-data-tag :options="dictDataMap['bill_item_class_dict']" :value="scope.row.itemClass"/>
      </template>
    </el-table-column>
    <el-table-column :align="'center'" label="模板主题名称" prop="title"/>
    <el-table-column :align="'center'" label="适用科室" prop="deptName"/>
    <el-table-column :align="'center'" label="发药药房" prop="storageName"/>
    <el-table-column :align="'center'" label="是否启用" prop="startFlag">
      <template #default="scope">
        <dict-data-tag :options="dictDataMap['sys_yes_no']" :value="scope.row.startFlag"/>
      </template>
    </el-table-column>
    <el-table-column v-if="trDisabled" :align="'center'" class-name="small-padding fixed-width" label="操作"
                     width="220">
      <template #default="scope">
        <el-button type="success" @click="handleUpdate(scope.row)">
          <template #icon>
            <i-ep-edit/>
          </template>
          修改
        </el-button>
        <el-button type="danger" @click="handleDelete(scope.row)">
          <template #icon>
            <i-ep-delete/>
          </template>
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum" :total="total"
              @pagination="getList"/>
</template>

<script setup lang="ts">
import {delTemplateMaster, selectPageTemplateMaster,} from "@/api/patient/templateMaster.ts";
import {useUserStoreHook} from "@/store/modules/user";
import {useDictStoreHook} from "@/store/modules/dict";

const {proxy} = getCurrentInstance() as any;

const props = defineProps({
  trDisabled: {
    type: Boolean,
    default: true
  },
  queryParams: {
    type: Object as PropType<TemplateMasterType>
  },
  refreshKey: {
    type: Number,
    default: 0
  },
})

watch(() => props.refreshKey, () => {
  getList()
})

const emits = defineEmits(["rowClick", "handleUpdate"])
const dictStore = useDictStoreHook();
let dictDataMap = ref(
  dictStore.isTypeGetData(
    "template_type",
    "sys_yes_no",
    "template_authority_type",
    "bill_item_class_dict"
  )
);
const adaptiveHeight = ref(props.trDisabled ? 560 : 820)
const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
const queryParamsRef = ref();
const templateMasterList = ref([]);
const multiple = ref(true);
const total = ref(0);
const ids = ref([]);

if (userInfo.orgId) {
  props.queryParams.orgId = userInfo.orgId
  getList()
} else {
  props.queryParams.orgId = ""
}

/** 查询模板主记录列表 */
function getList() {
  if (props.queryParams.orgId) {
    selectPageTemplateMaster(props.queryParams).then((response: any) => {
      templateMasterList.value = response.rows;
      total.value = response.total;
    });
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  if (!props.queryParams.orgId) {
    return ElMessage.error("请先选择机构");
  }
  props.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParamsRef.value.resetFields();
  // proxy.resetForm("queryRef");
  handleQuery();
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _templateMasterIds = row.templateMasterId || ids.value;
  ElMessageBox.confirm("是否确认删除模板数据项？", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delTemplateMaster(_templateMasterIds).then(() => {
        getList();
        ElMessage.success("删除成功");
      });
    })
    .catch(() => {
    });
}

// 机构树点击事件
function orgNodeClick(data, _e) {
  props.queryParams.orgId = data.orgId;
  if (props.queryParams.orgId) {
    getList();
  }
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.templateMasterId);
  multiple.value = !selection.length;
}


// 主表行点击事件
function rowClick(row: any) {
  emits("rowClick", row)
}

function handleUpdate(row: any) {
  emits("handleUpdate", row)
}

getList();

defineExpose({
  getList
})

</script>

<style scoped></style>
